package com.Darkra1Zzz.service.impl;

import com.Darkra1Zzz.dao.UserDao;
import com.Darkra1Zzz.entity.PageInfo;
import com.Darkra1Zzz.entity.User;
import com.Darkra1Zzz.service.UserService;
import com.Darkra1Zzz.util.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    //注入dao
    @Resource
    private UserDao userDao;

//    @Override
//    public PageInfo<User> findPageInfo(String name, Integer id, Integer phone,
//                                       String email, String vip,Integer pageIndex, Integer pageSize) {
    @Override
    public PageInfo<User> findPageInfo(User user, Integer pageIndex, Integer pageSize) {
        PageInfo<User> pageInfo = new PageInfo<User>();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        //获取总条数
//        Integer totalCount = userDao.countAllUser(name, id, phone, email, vip);

        Integer totalCount = userDao.countAllUser(user);

        if (totalCount > 0) {
            pageInfo.setTotalCount(totalCount);
            //每一页显示信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<User> UserList = userDao.findUserList(user.getName(),user.getPhone(),user.getEmail(),user.getVip(),
                    (pageInfo.getPageIndex() - 1) * pageInfo.getPageSize(), pageInfo.getPageSize());
            pageInfo.setList(UserList);
        }
        return pageInfo;
    }

    @Override
    public int deleteUser(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public int addUser(User user) {
        user.setPwd(MD5.encrypByMd5(MD5.encrypByMd5("123456")));
        return userDao.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.update(user);
    }

    @Override
    public User findUserById(Integer id) {
        User user=userDao.findUserById(id);
        return user;
    }

    @Override
    public List<User> getAll() {
        List<User> userList = userDao.findAllUser();
        return userList;
    }
}
